home *** CD-ROM | disk | FTP | other *** search
/ Revista do CD-ROM 97 / CD-ROM 97 / CD-ROM 97.iso / internet / ghostzilla / ghsetup.exe / chrome / inspector.jar / content / inspector / ViewerRegistry.js < prev   
Encoding:
Text File  |  2002-04-09  |  7.9 KB  |  270 lines

  1. /* ***** BEGIN LICENSE BLOCK *****
  2.  * Version: NPL 1.1/GPL 2.0/LGPL 2.1
  3.  *
  4.  * The contents of this file are subject to the Netscape Public License
  5.  * Version 1.1 (the "License"); you may not use this file except in
  6.  * compliance with the License. You may obtain a copy of the License at
  7.  * http://www.mozilla.org/NPL/
  8.  *
  9.  * Software distributed under the License is distributed on an "AS IS" basis,
  10.  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
  11.  * for the specific language governing rights and limitations under the
  12.  * License.
  13.  *
  14.  * The Original Code is mozilla.org code.
  15.  *
  16.  * The Initial Developer of the Original Code is 
  17.  * Netscape Communications Corporation.
  18.  * Portions created by the Initial Developer are Copyright (C) 2001
  19.  * the Initial Developer. All Rights Reserved.
  20.  *
  21.  * Contributor(s):
  22.  *   Joe Hewitt <hewitt@netscape.com> (original author)
  23.  *
  24.  *
  25.  * Alternatively, the contents of this file may be used under the terms of
  26.  * either the GNU General Public License Version 2 or later (the "GPL"), or
  27.  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
  28.  * in which case the provisions of the GPL or the LGPL are applicable instead
  29.  * of those above. If you wish to allow use of your version of this file only
  30.  * under the terms of either the GPL or the LGPL, and not to allow others to
  31.  * use your version of this file under the terms of the NPL, indicate your
  32.  * decision by deleting the provisions above and replace them with the notice
  33.  * and other provisions required by the GPL or the LGPL. If you do not delete
  34.  * the provisions above, a recipient may use your version of this file under
  35.  * the terms of any one of the NPL, the GPL or the LGPL.
  36.  *
  37.  * ***** END LICENSE BLOCK ***** */
  38.  
  39. /***************************************************************
  40. * ViewerRegistry -----------------------------------------------
  41. *  The central registry where information about all installed
  42. *  viewers is kept.  
  43. * - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
  44. * REQUIRED IMPORTS:
  45. *   chrome://inspector/content/jsutil/xpcom/XPCU.js
  46. *   chrome://inspector/content/jsutil/rdf/RDFU.js
  47. ****************************************************************/
  48.  
  49. //////////// global variables /////////////////////
  50.  
  51. //////////// global constants ////////////////////
  52.  
  53. const kViewerURLPrefix = "chrome://inspector/content/viewers/";
  54. const kViewerRegURL  = "resource:///res/inspector/viewer-registry.rdf";
  55.  
  56. ////////////////////////////////////////////////////////////////////////////
  57. //// class ViewerRegistry
  58.  
  59. function ViewerRegistry() // implements inIViewerRegistry
  60. {
  61.   this.mViewerHash = {};
  62. }
  63.  
  64. ViewerRegistry.prototype = 
  65. {
  66.   ////////////////////////////////////////////////////////////////////////////
  67.   //// interface inIViewerRegistry
  68.  
  69.   // not yet formalized...
  70.   
  71.   ////////////////////////////////////////////////////////////////////////////
  72.   //// Initialization
  73.  
  74.   mDS: null,
  75.   mObserver: null,
  76.   mViewerDS: null,
  77.   mViewerHash: null,
  78.   mFilters: null,
  79.   
  80.   get url() { return this.mURL; },
  81.  
  82.   //// Loading Methods
  83.  
  84.   load: function(aURL, aObserver)
  85.   {
  86.     this.mURL = aURL;
  87.     this.mObserver = aObserver;
  88.     RDFU.loadDataSource(aURL, new ViewerRegistryLoadObserver(this));
  89.   },
  90.  
  91.   onError: function(aStatus, aErrorMsg)
  92.   {
  93.     this.mObserver.onViewerRegistryLoadError(aStatus, aErrorMsg);
  94.   },
  95.  
  96.   onLoad: function(aDS)
  97.   {
  98.     this.mDS = aDS;
  99.     this.prepareRegistry();
  100.     this.mObserver.onViewerRegistryLoad();
  101.   },
  102.  
  103.   prepareRegistry: function()
  104.   {
  105.     this.mViewerDS = RDFArray.fromContainer(this.mDS, "inspector:viewers", kInspectorNSURI);
  106.     
  107.     // create and cache the filter functions
  108.     var js, fn;
  109.     this.mFilters = [];
  110.     for (var i = 0; i < this.mViewerDS.length; ++i) {
  111.       js = this.getEntryProperty(i, "filter");
  112.       try {
  113.         fn = new Function("doesQI", "object", js);
  114.       } catch (ex) {
  115.         fn = new Function("return false");
  116.         debug("### ERROR - Syntax error in filter for viewer \"" + this.getEntryProperty(i, "description") + "\"\n");
  117.       }
  118.       this.mFilters.push(fn);
  119.     }
  120.   },
  121.  
  122.   ///////////////////////////////////////////////////////////////////////////
  123.   // Returns the absolute url where the xul file for a viewer can be found.
  124.   //
  125.   // @param long aIndex - the index of the entry representing the viewer
  126.   // @return wstring - the fully cannonized url
  127.   ///////////////////////////////////////////////////////////////////////////
  128.   getEntryURL: function(aIndex)
  129.   {
  130.     var uid = this.getEntryProperty(aIndex, "uid");
  131.     return kViewerURLPrefix + uid + "/" + uid + ".xul";
  132.   },
  133.  
  134.   //// Lookup Methods
  135.  
  136.   ///////////////////////////////////////////////////////////////////////////
  137.   // Searches the viewer registry for all viewers that can view a particular
  138.   // object.
  139.   //
  140.   // @param Object aObject - the object being searched against
  141.   // @return nsIRDFResource[] - array of entries in the viewer registry
  142.   ///////////////////////////////////////////////////////////////////////////
  143.   findViewersForObject: function(aObject)
  144.   {
  145.     // check each entry in the registry
  146.     var len = this.mViewerDS.length;
  147.     var entry;
  148.     var urls = [];
  149.       for (var i = 0; i < len; ++i) {
  150.       if (this.objectMatchesEntry(aObject, i)) {
  151.         if (this.getEntryProperty(i, "important")) {
  152.           urls.unshift(i); 
  153.         } else {
  154.           urls.push(i);
  155.         }
  156.       }
  157.        }
  158.  
  159.     return urls;
  160.   },
  161.  
  162.   ///////////////////////////////////////////////////////////////////////////
  163.   // Determines if an object is eligible to be viewed by a particular viewer.
  164.   //
  165.   // @param Object aObject - the object being checked for eligibility
  166.   // @param long aIndex - the index of the entry
  167.   // @return boolean - true if object can be viewed
  168.   ///////////////////////////////////////////////////////////////////////////
  169.   objectMatchesEntry: function(aObject, aIndex)
  170.   {
  171.     if (!aObject) return false;
  172.     return this.mFilters[aIndex](this.doesQI, aObject);
  173.   },
  174.   
  175.   doesQI: function(aObject, aInterface)
  176.   {
  177.     if (!("QueryInterface" in aObject)) return false;
  178.     
  179.     try {
  180.       var result = aObject.QueryInterface(Components.interfaces[aInterface]);
  181.       return true;
  182.     } catch (ex) {
  183.       return false;
  184.     }
  185.   },
  186.  
  187.   ///////////////////////////////////////////////////////////////////////////
  188.   // Notifies the registry that a viewer has been instantiated, and that
  189.   // it corresponds to a particular entry in the viewer registry.
  190.   //
  191.   // @param 
  192.   ///////////////////////////////////////////////////////////////////////////
  193.   cacheViewer: function(aViewer, aIndex)
  194.   {
  195.     var uid = this.getEntryProperty(aIndex, "uid");
  196.     this.mViewerHash[uid] = { viewer: aViewer, entry: aIndex };
  197.   },
  198.  
  199.   uncacheViewer: function(aViewer)
  200.   {
  201.     delete this.mViewerHash[aViewer.uid];
  202.   },
  203.   
  204.   // for previously loaded viewers only
  205.   getViewerByUID: function(aUID)
  206.   {
  207.     return this.mViewerHash[aUID].viewer;
  208.   },
  209.  
  210.   // for previously loaded viewers only
  211.   getEntryForViewer: function(aViewer)
  212.   {
  213.     return this.mViewerHash[aViewer.uid].entry;
  214.   },
  215.  
  216.   // for previously loaded viewers only
  217.   getEntryByUID: function(aUID)
  218.   {
  219.     return this.mViewerHash[aUID].aIndex;
  220.   },
  221.  
  222.   getEntryProperty: function(aIndex, aProp)
  223.   {
  224.     return this.mViewerDS.get(aIndex, aProp);
  225.   },
  226.  
  227.   getEntryCount: function()
  228.   {
  229.     return this.mViewerDS.length;
  230.   },
  231.  
  232.   ////////////////////////////////////////////////////////////////////////////
  233.   //// Viewer Registration
  234.  
  235.   addNewEntry: function(aUID, aDescription, aFilter)
  236.   {
  237.   },
  238.  
  239.   removeEntry: function(aIndex)
  240.   {
  241.   },
  242.  
  243.   saveRegistry: function()
  244.   {
  245.   }
  246.  
  247. };
  248.  
  249. ////////////////////////////////////////////////////////////////////////////
  250. //// Listener Objects
  251.  
  252. function ViewerRegistryLoadObserver(aTarget) 
  253. {
  254.   this.mTarget = aTarget;
  255. }
  256.  
  257. ViewerRegistryLoadObserver.prototype = {
  258.   mTarget: null,
  259.  
  260.   onError: function(aStatus, aErrorMsg) 
  261.   {
  262.     this.mTarget.onError(aStatus, aErrorMsg);
  263.   },
  264.  
  265.   onDataSourceReady: function(aDS) 
  266.   {
  267.     this.mTarget.onLoad(aDS);
  268.   }
  269. };
  270.